package gotengine

import (
	"fmt"
	"strings"
	"time"

	"gitee.com/tommickey/gose/v3/gose/gotlib"
)

// GetDefaultEngineParams 此函数为提供P默认数据的参考，实际项目中需要定制
func GetDefaultEngineParams() (P map[string]interface{}) {
	P = make(map[string]interface{})
	//P["_NowTime"] = time.Now().Format("2006-01-02 15:04:05")
	//P["_NowTimeString"] = time.Now().Format("20060102150405")
	//P["_UserName"] = "Tommy"
	//P["_UserId"] = 2
	return
}

func AddDefaultEngineAPIs(mapAPI map[string]interface{}) (apis map[string]interface{}) {
	apis = make(map[string]interface{})
	apis = GetDefaultEngineAPIs()
	for k, v := range mapAPI {
		if k != "" {
			apis[k] = v
		}
	}
	return
}

// GetDefaultEngineAPIs 此函数为提供默认函数清单，实际项目中需要定制
func GetDefaultEngineAPIs() (apis map[string]interface{}) {
	apis = make(map[string]interface{})
	apis["_Print"] = fmt.Print
	apis["_Println"] = fmt.Println
	apis["_QueryMap"] = gotlib.QueryMapNoCTX
	apis["_Int64"] = gotlib.Int64
	apis["_String"] = gotlib.String
	apis["_InArray"] = gotlib.InArray
	apis["_Index"] = strings.Index
	apis["_Split"] = strings.Split
	apis["_StrMiddle"] = gotlib.StrMiddle
	apis["_StrLeft"] = gotlib.StrLeft
	apis["_StrLeftBack"] = gotlib.StrLeftBack
	apis["_StrRight"] = gotlib.StrRight
	apis["_StrRightBack"] = gotlib.StrRightBack

	apis["_PadLeft"] = gotlib.PadLeft

	apis["_Length"] = gotlib.Length
	apis["_MarshalTo_String"] = gotlib.MarshalToString
	apis["_UnMarshalToMap"] = gotlib.UnMarshalToMap
	apis["_Calc"] = gotlib.Calc
	apis["_CalcString"] = gotlib.CalcString

	apis["_CalcTime"] = gotlib.CalcTime // 时间加年月日时分秒

	apis["_DefStringA"] = DefStringA
	apis["_DefInt64A"] = DefInt64A
	apis["_DefFloat64A"] = DefFloat64A
	apis["_DefMapStrInterfaceA"] = DefMapStrInterfaceA

	//apis[FixParamName] = GetDefaultEngineParams()
	apis["_NowTime"] = time.Now().Format("2006-01-02 15:04:05")
	apis["_NowTimeString"] = time.Now().Format("20060102150405")

	return apis
}
func DefStringA(iLen int, any ...interface{}) (ret []string) {
	iAny := len(any)
	for i := 0; i < iLen; i++ {
		if iAny <= i {
			ret = append(ret, "")
		} else {
			ret = append(ret, gotlib.String(any[i]))
		}
	}
	return
}
func DefInt64A(iLen int, any ...interface{}) (ret []int64) {
	iAny := len(any)
	for i := 0; i < iLen; i++ {
		if iAny <= i {
			ret = append(ret, 0)
		} else {
			ret = append(ret, gotlib.Int64(any[i]))
		}
	}
	return
}
func DefFloat64A(iLen int, any ...interface{}) (ret []float64) {
	iAny := len(any)
	for i := 0; i < iLen; i++ {
		if iAny <= i {
			ret = append(ret, 0.0)
		} else {
			ret = append(ret, gotlib.Float64(any[i]))
		}
	}
	return
}

// DefMapStrInterfaceA engine初始化map变量，any两个一组kv
func DefMapStrInterfaceA(iLen int, any ...interface{}) (ret []map[string]interface{}) {
	iAny := len(any)
	for i := 0; i < iLen; i++ {
		m := make(map[string]interface{})
		if i > iAny/2-1 {
			ret = append(ret, m)
		} else {
			m[any[2*i].(string)] = any[2*i+1]
			ret = append(ret, m)
		}
	}
	return
}
